package com.luyuan.crm.question.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.question.model.QuestionExtendField;
import com.luyuan.crm.question.model.QuestionExtend;
import com.luyuan.crm.question.service.QuestionExtendFieldService;
import com.luyuan.crm.question.service.QuestionExtendService;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;

@Component("questionExtendFieldAction")
@Scope("view")
public class QuestionExtendFieldAction extends BaseAction implements InitializingBean {

    private QuestionExtend questionExtend;

    private QuestionExtendField questionExtendField = new QuestionExtendField();

    @Resource(name = "questionExtendService")
    private QuestionExtendService questionExtendService;

    @Resource(name = "questionExtendFieldService")
    private QuestionExtendFieldService questionExtendFieldService;

    public void addQuestionExtendField() {
        questionExtendField.setQuestionExtend(questionExtend);
        questionExtendFieldService.save(questionExtendField);
    }

    public DataModel getTableModel() {
        TablePage tablePage = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder tableOrder : tableOrders) {
            Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(QuestionExtendField.class);
        RestrictionTemplate r = new RestrictionTemplate(false);
        c.add(r.eq("questionExtend.id", questionExtend.getId()));

        Page page = questionExtendFieldService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void delQuestionExtendField(int id) {
        questionExtendFieldService.remove(id);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String id = getRequestParameter("uuid");
        questionExtend = questionExtendService.getById(Integer.parseInt(id));
    }

    public QuestionExtend getQuestionExtend() {
        return questionExtend;
    }

    public void setQuestionExtend(QuestionExtend questionExtend) {
        this.questionExtend = questionExtend;
    }

    public QuestionExtendField getQuestionExtendField() {
        return questionExtendField;
    }

    public void setQuestionExtendField(QuestionExtendField questionExtendField) {
        this.questionExtendField = questionExtendField;
    }

}
